package com.lfk.justwetools.Component.CartoonView;

import ohos.agp.utils.Point;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Image;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.Text;

import com.lfk.justwetools.ResourceTable;
import com.lfk.justwetools.Utils.ImageLoader;

import java.util.ArrayList;

/**
 * Created by liufengkai on 16/2/22.
 */
public class CartoonAdapter extends BaseItemProvider {
    private ArrayList<String> userList;
    private LayoutScatter mInflater;
    private ImageLoader mImageLoader;
    private Context mContext;
    private int width, height;

    public CartoonAdapter(Context context, ArrayList<String> userList) {
        this.userList = userList;
        this.mContext = context;
        this.mInflater = LayoutScatter.getInstance(context);
        this.mImageLoader = ImageLoader.getInstance(mContext,4, ImageLoader.Type.LIFO);
    }

    private class ViewHolder {
        public Image imageView;
        public Text textView;
    }

    @Override
    public int getCount() {
        return userList.size();
    }

    @Override
    public Object getItem(int position) {
        return userList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertView, ComponentContainer parent) {
        ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.parse(ResourceTable.Layout_cartoon_item, null, false);
            holder.imageView = (Image) convertView.findComponentById(ResourceTable.Id_cartoon_img);

            Point screenSize = new Point();
            DisplayManager.getInstance().getDefaultDisplay(parent.getContext()).get().getSize(screenSize);
            DependentLayout.LayoutConfig params = new DependentLayout.LayoutConfig(screenSize.getPointXToInt(),
                    screenSize.getPointYToInt());

            holder.imageView.setLayoutConfig(params);

            holder.textView = (Text) convertView.findComponentById(ResourceTable.Id_cartoon_num);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.textView.setText(String.valueOf(position));

        mImageLoader.loadImage(userList.get(position),
                holder.imageView);

        holder.textView.setText("");

        return convertView;
    }
}
